/*
============================================================================
 Name        : pd_gui.cpp
 Author      : Jian(raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : pd_gui.cpp - source file
============================================================================
*/

// INCLUDE FILES

#include "pd_gui.h"
#include "PdBitmapGc.h"
#include "PdImageDecoder.h"
#include "PdDefine.h"


/* just for check */
static pd_gc* __cur_gc = NULL ;

pd_gc* pd_gc_create(pd_gc_type type) 
{
    pd_gc* sGc = NULL ;
    
    TRAPD(err, sGc = (pd_gc*)CPdBitmapGc::NewL(EColor64K)) ;

    if (err != KErrNone) {
        DD_LOG("pd_gc_create create type = %d error = %d", type, err) ;
        return NULL ;
    }

    return sGc ;
}

void pd_gc_destroy(pd_gc* gc) 
{
    __DD_ASSERT_DEFENSE(gc, return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;
}

void pd_gc_activate(pd_gc* gc) 
{
    __DD_ASSERT_DEFENSE(gc && (__cur_gc == DD_NULL), return) ;
    __cur_gc = gc ;
}

void pd_gc_deactivate(pd_gc* gc) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;
    __cur_gc = DD_NULL ;
}

void pd_gc_flush(pd_gc* gc) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;
}

void pd_set_font(pd_gc* gc, pd_font* font) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;
    __DD_ASSERT_DEFENSE(font, return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;
    CFont* sFont     = (CFont*)font ;

    sGc->Gc()->UseFont(sFont) ;
}

void pd_unset_cur_font(pd_gc* gc, pd_font* cur_font) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;
    __DD_ASSERT_DEFENSE(cur_font, return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;
    sGc->Gc()->DiscardFont() ;
}

void pd_draw_text(pd_gc* gc, dd_pos pos, const DD_UNICODE* text, DD_UINT text_len, dd_color color) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;
    __DD_CHECK_PTR(text, DD_UNICODE, return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;
    sGc->Gc()->SetPenStyle(CGraphicsContext::ESolidPen) ;
    sGc->Gc()->SetPenColor(__ToTRgb(color)) ;
    TPtrC pText(text, text_len) ;
    sGc->Gc()->DrawText(pText, __ToTPoint(pos)) ;
}

void pd_draw_image(pd_gc* gc, dd_pos pos, pd_image* image) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;
    __DD_ASSERT_DEFENSE(image && pd_img_is_ready(image), return) ;

    CPdBitmapGc* sGc         = (CPdBitmapGc*)gc ;
    CPdImageDecoder* decoder = (CPdImageDecoder*)image ;

    sGc->Gc()->SetBrushStyle(CGraphicsContext::ENullBrush) ;

    if (decoder->Mask()) 
        sGc->Gc()->BitBltMasked(__ToTPoint(pos), decoder->Bitmap(), TRect(decoder->ImageSize()), decoder->Mask(), ETrue) ;
    else
        sGc->Gc()->BitBlt(__ToTPoint(pos), decoder->Bitmap(), TRect(decoder->ImageSize())) ;
}

void pd_draw_rect(pd_gc* gc, dd_rect rect, dd_size angle, dd_color color) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;

    sGc->Gc()->SetPenStyle(CGraphicsContext::ENullPen) ;
    sGc->Gc()->SetBrushStyle(CGraphicsContext::ESolidBrush) ;
    sGc->Gc()->SetBrushColor(__ToTRgb(color)) ;
    sGc->Gc()->DrawRoundRect(__ToTRect(rect), __ToTSize(angle)) ;
}

void pd_draw_border(pd_gc* gc, dd_rect rect, dd_size angle, dd_color color) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;

    sGc->Gc()->SetPenStyle(CGraphicsContext::ESolidPen) ;
    sGc->Gc()->SetBrushStyle(CGraphicsContext::ENullBrush) ;
    sGc->Gc()->SetPenColor(__ToTRgb(color)) ;
    sGc->Gc()->DrawRoundRect(__ToTRect(rect), __ToTSize(angle)) ;
}

void pd_draw_line(pd_gc* gc, dd_pos pos1, dd_pos pos2, dd_color color) 
{
    __DD_ASSERT_DEFENSE(gc && (gc == __cur_gc), return) ;

    CPdBitmapGc* sGc = (CPdBitmapGc*)gc ;

    sGc->Gc()->SetPenStyle(CGraphicsContext::ESolidPen) ;
    sGc->Gc()->SetPenColor(__ToTRgb(color)) ;
    sGc->Gc()->DrawLine(__ToTPoint(pos1), __ToTPoint(pos2)) ;
}

void pd_merge_gc(pd_gc* bg_gc, pd_gc* front_gc, dd_rect rect, DD_UINT8 transparent) 
{
    __DD_ASSERT_DEFENSE(bg_gc && front_gc, return) ;
    
    CPdBitmapGc::Merge((CPdBitmapGc*)bg_gc, (CPdBitmapGc*)front_gc, __ToTRect(rect), transparent) ;
}

/* End of File */
